/*
 * @Author: your name
 * @Date: 2022-01-03 17:33:19
 * @LastEditTime: 2022-01-04 17:01:47
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \human resource\src\store\modules\permission.js
 */
import { asyncRoutes, constantRoutes } from '@/router'
import store from '@/store'
const state = {
    routes: []
}
const mutations = {
    setRoutes(state, data) {
        // 真正的菜单应该由静态路由+有权限的动态路由拼接而成
        // 有权限的动态路由已经从 data 传入
        state.routes = [...constantRoutes, ...data]
    }
}
const actions = {
    filterRoutes() {
        const menus = store.state.user.profile.roles.menus
        const routes = asyncRoutes.filter(item => menus.includes(item.name))

        // 本来, 这个逻辑实在导航守卫的, 封装在这里是为了同时在两个逻辑上共享
        // 1. 回复url访问(靠 return 到导航守卫调用 router.addRoutes)
        // 2. 希望拼接出一个菜单列表,替换原来默认的路由配置(调用 mutations)
        store.commit('permission/setRoutes', routes)
        return routes
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}